from app.services.business.gaolu_diagnose.ind.abstract_ind import AbstractInd
from typing import List


class CG_LT_GL_GL04_Rshuiwencha(AbstractInd):

    def get_name(self):
        return "CG_LT_GL_GL04_Rshuiwencha"

    def get_sql(self, params):

        sql_LQSPSWD = """
        SELECT mean(value) AS CG_LT_GL_GL04_RLQSPSWD   FROM "CG_LT_GL_GL04_LQSPSWD"
         where time > '{dt_start_tz}' and time < '{dt_end_tz}'
        """.format(**params)

        sql_LQSGSWD = """
         SELECT mean(value) AS CG_LT_GL_GL04_RLQSGSWD   FROM "CG_LT_GL_GL04_LQSGSWD"
         where time > '{dt_start_tz}' and time < '{dt_end_tz}'
        """.format(**params)

        return [sql_LQSPSWD, sql_LQSGSWD]

    @AbstractInd.cache_wrapper
    def get_result(self):
        sql_list: List[str] = self.get_sql(self.params)
        client = self.influx_client
        sql_LQSPSWD = sql_list[0]
        LQSPSWD_result_set = list(client.query(sql_LQSPSWD))
        LQSPSWD_value = self.get_influx_result(LQSPSWD_result_set, "CG_LT_GL_GL04_RLQSPSWD")

        sql_LQSGSWD = sql_list[1]
        LQSGSWD_result_set = list(client.query(sql_LQSGSWD))
        LQSGSWD_value = self.get_influx_result(LQSGSWD_result_set, "CG_LT_GL_GL04_RLQSGSWD")

        # 先取两个值，然后相减CG_LT_GL_GL04_Rshuiwencha=CG_LT_GL_GL04_RLQSPSWD-CG_LT_GL_GL04_RLQSGSWD

        if LQSPSWD_value is None: LQSPSWD_value = 0
        if LQSGSWD_value is None: LQSGSWD_value = 0

        return LQSPSWD_value - LQSGSWD_value
